{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LinearRegression\n",
    "### https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "from sklearn.linear_model import LinearRegression\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_data = np.random.random_sample((20, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.27001394, 0.26644793],\n",
       "       [0.81530155, 0.41199833],\n",
       "       [0.90090939, 0.60278144],\n",
       "       [0.22089745, 0.57292765],\n",
       "       [0.39699127, 0.83810315],\n",
       "       [0.11326951, 0.93362151],\n",
       "       [0.91498974, 0.90899279],\n",
       "       [0.79160141, 0.04334325],\n",
       "       [0.80842485, 0.23024809],\n",
       "       [0.33509719, 0.59181763],\n",
       "       [0.05818222, 0.2235917 ],\n",
       "       [0.04174783, 0.62949153],\n",
       "       [0.35560399, 0.24130075],\n",
       "       [0.45696207, 0.92612841],\n",
       "       [0.55647435, 0.07499975],\n",
       "       [0.39967875, 0.50945095],\n",
       "       [0.77814294, 0.74157317],\n",
       "       [0.35658407, 0.10417907],\n",
       "       [0.25323549, 0.14249189],\n",
       "       [0.5489488 , 0.58350335]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = random_data[:, 0] #first column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def assuming_function(x):\n",
    "    return 10.11 * x + 5 - random.randint(-5, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = [assuming_function(x) for x in X]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x296a36949b0>"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAVm0lEQVR4nO3df4ylV33f8fcny9JOwO0YvBDv2Itp5aya2MVLRlbQqhRC8BoL4a1FU1tNalQnWwhUoYlWeBspjeAPW2yTSK2jmE1sYSrqQMp6sWLDYtVUhoofHntt1mC2dlwIO2PZG8xiENNmvf32j7kDM+M7M3funXvvzHPfL2k0zz3Pee5z5uzsZ557nnPuTVUhSWqunxp2AyRJ/WXQS1LDGfSS1HAGvSQ1nEEvSQ1n0EtSw60a9EkuTPL5JI8n+XqS32qVvyLJfUmeaH0/d5njr2/VeSLJ9ev9A0iSVpbV5tEnOR84v6oeTnIO8BCwF3gX8FxV3ZzkRuDcqvrAkmNfAUwBk0C1jv2Fqvreuv8kkqS2Vr2ir6qnq+rh1vYPgMeBCeBq4I5WtTuYC/+l9gD3VdVzrXC/D7hyPRouSerMS9ZSOclFwC7gK8Crq+ppmPtjkORVbQ6ZAL6z4PHJVtmKzjvvvLrooovW0jRJGmkPPfTQ31TVtnb7Og76JC8HPgW8v6qeT9LRYW3K2o4VJdkH7APYsWMHU1NTnTZNkkZekm8vt6+jWTdJtjIX8h+vqsOt4mda4/fz4/jPtjn0JHDhgscXADPtzlFVh6pqs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neW2SlzJ3s/XuJXXuBq5vbb8TuL9ad1QaatU+aQ1VfIS5kG/6+OuK/VFV36+q86rqoqq6iLl7Fu+oqqnhNLfvOvk/c4S5G/YkOY+5oZynBtrKweqkT/4aeAtAkn/EXNCfGmgrh2BDBH1rzP19wFHgceCTVfX1JB9M8o5WtduAVyZ5EvhtYNnpdU3QYZ8cBF4O/EWSR5Is/aVujA77Y2R02B9Hge8m+QbweWB/VX13OC3uvw775HeA30jyKHAn8K6GXzACroyVpMbbEFf0kqT+MeglqeEMeklqOINekhrOoJekhjPoJanhDHpJajiDXpIa7v8DzQlzLkOcQqIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20,)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.27001394, 0.81530155, 0.90090939, 0.22089745, 0.39699127,\n",
       "       0.11326951, 0.91498974, 0.79160141, 0.80842485, 0.33509719,\n",
       "       0.05818222, 0.04174783, 0.35560399, 0.45696207, 0.55647435,\n",
       "       0.39967875, 0.77814294, 0.35658407, 0.25323549, 0.5489488 ])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.27001394],\n",
       "       [0.81530155],\n",
       "       [0.90090939],\n",
       "       [0.22089745],\n",
       "       [0.39699127],\n",
       "       [0.11326951],\n",
       "       [0.91498974],\n",
       "       [0.79160141],\n",
       "       [0.80842485],\n",
       "       [0.33509719],\n",
       "       [0.05818222],\n",
       "       [0.04174783],\n",
       "       [0.35560399],\n",
       "       [0.45696207],\n",
       "       [0.55647435],\n",
       "       [0.39967875],\n",
       "       [0.77814294],\n",
       "       [0.35658407],\n",
       "       [0.25323549],\n",
       "       [0.5489488 ]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.reshape(-1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = np.array(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg = LinearRegression().fit(X.reshape(-1, 1), y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6321607422846118"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reg.score(X.reshape(-1, 1), y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([14.51250288])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reg.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.336754521192951"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reg.intercept_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    return reg.coef_ * x + reg.intercept_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x296a3992e80>]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAd90lEQVR4nO3de3hcdb3v8ffXUiQiEugFm5Ry30Xu3YRy6UG5NqVCKWyOlqOCRzY9uDcKj9IHCl4BabVHefSASjegeLayQS2hXNvaIgU2VQIBCy21pbtIktIrAZF4aMP3/LHWkMnMJJnMZa2ZNZ/X8+TJzG/WZL5dTT/9zW991xpzd0REJLk+EHcBIiJSXgp6EZGEU9CLiCScgl5EJOEU9CIiCaegFxFJuEGD3sz2NbPHzGy1mb1kZleE43ub2RIzWxt+36uf518cbrPWzC4u9R9AREQGZoP10ZvZGGCMuz9nZnsAzwLTgc8D2919rpldA+zl7ldnPHdvoBVoAjx87rHu/kbJ/yQiIpLToDN6d9/o7s+Ft/8KrAYagXOBu8LN7iII/0zNwBJ33x6G+xJgSikKFxGR/OwylI3NbH9gAvAHYB933wjBfwZmNjrHUxqB19Lut4djAxo5cqTvv//+QylNRKSmPfvss1vdfVSux/IOejP7MPBb4Ep3f8vM8npajrGca0VmNhOYCTBu3DhaW1vzLU1EpOaZ2av9PZZX142ZDScI+V+6+4JweFO4fp9ax9+c46ntwL5p98cCnblew93nu3uTuzeNGpXzPyURESlAPl03BtwBrHb3H6Q9tBBIddFcDNyf4+mLgMlmtlfYlTM5HBMRkYjkM6OfBHwOOM3Mng+/pgJzgTPNbC1wZngfM2sys9sB3H07cAPwTPh1fTgmIiIRGbS9Mg5NTU2uNXoRkfyZ2bPu3pTrMZ0ZKyKScENqrxQRkaFraetg3qI1dHZ101Bfx6zm8UyfMGinecko6EVEyqilrYPZC1b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Ugn/1xKXCru78B4O6bI64xavnsEwc+Et7eE+iMsL7YVErQNwKvpd1vD8dybuPuO4E3gRGRVBePfPZJukuAR8paUbwG3R9mNgHY190fjLKwmOTz+/EPwD+Y2VNmtsLMpkRWXTzy2SffAj5rZu3Aw8CXoiktXpXywSO5ZuaZ7UD5bJMkef95zeyzQBPwibJWFK8B94eZfQC4Gfh8VAXFLJ/fj10Ilm9OIXi394SZHeHuXWWuLS757JMLgZ+7+/fN7ETg/4b75L3ylxefSpnRtwP7pt0fS/Zbqve3MbNdCN52bY+kunjks08wszOA64Bp7v7/IqotDoPtjz2AI4Dfm9kG4ARgYYIPyOb7b+Z+d9/h7v8FrCEI/qTKZ59cAtwL4O5PA7sRXAcn0Sol6J8BDjGzA8xsV4KDrQsztlkIXBzevgBY5uERlYQadJ+ESxW3EYR80tdfB9wf7v6mu4909/3dfX+CYxbT3L01nnLLLp9/My0EB+wxs5EESznrI60yWvnsk78ApwOY2ccIgn5LpFXGoCKCPlxzvxxYBKwG7nX3l8zsejObFm52BzDCzNYBXwH6ba9Lgjz3yTzgw8Cvzex5M8v8pU6MPPdHzchzfywCtpnZKuAxYJa7b4un4vLLc598FbjUzF4A7gY+n/AJI6AzY0VEEq8iZvQiIlI+CnoRkYRT0IuIJJyCXkQk4RT0IiIJp6AXEUk4Bb2ISMIp6EVEEu7/A0VbS1C0vEg8AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X, y)\n",
    "plt.plot(X, f(X), color='red')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to implement a KNN model\n",
    "#### K-Nearest Neighbours ，K值邻近算法，是一个简单的，常被用于分类问题的算法。\n",
    "### https://docs.scipy.org/doc/scipy/reference/spatial.distance.html\n",
    "$$ 1 - \\frac{u \\cdot v}{||u||_2 ||v||_2} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.spatial.distance import cosine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "#define model\n",
    "def model(X, y):\n",
    "    return [(Xi, yi) for Xi, yi in zip(X, y)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "def distance(u, v):\n",
    "    return cosine(u, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(x, k=5):\n",
    "    most_similars = sorted(model(X, y), key=lambda xi: distance(xi[0], x))[:k]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
